package com.vito.framework.system.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vito.framework.base.exception.CommonException;
import com.vito.framework.base.service.impl.BaseServiceImpl;
import com.vito.framework.system.dict.mapper.DictManageMapper;
import com.vito.framework.system.dict.model.SysDict;
import com.vito.framework.system.dict.service.DictManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictManageServiceImpl extends BaseServiceImpl<DictManageMapper, SysDict> implements DictManageService {

	@Autowired
	DictManageMapper dictManageMapper;

	@Override
	public IPage<SysDict> getDictManageList(Map<String, Object> paramMap)
			throws CommonException {
		Page<SysDict> page = new Page<>((Integer) paramMap.get("param_curpage"), (Integer) paramMap.get("param_ipagecount"));
		return dictManageMapper.selectPage(page, new LambdaQueryWrapper<SysDict>()
				.like(SysDict::getPtypeCode, paramMap.get("param_keyword"))
				.or()
				.like(SysDict::getPtypeName, paramMap.get("param_keyword"))
				.or()
				.like(SysDict::getDictName, paramMap.get("param_keyword"))
		);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void addSave(SysDict sysDict) throws CommonException {
		String[] code = sysDict.getDictCode().split("\\,");
		String[] name = sysDict.getDictName().split("\\,");
		String[] status = sysDict.getStatus().split("\\,");
		String[] sort = String.valueOf(sysDict.getSort()).split("\\,");
		List<SysDict> toInsertList = new ArrayList<>();
		for(int i = 0; i < code.length; i++){
			SysDict dict = new SysDict();
			Map<String, Object> map = new HashMap<String, Object>();
			dict.setDictCode(code[i].trim());
			dict.setDictName(name[i].trim());
			dict.setStatus(status[i].trim());
			dict.setSort(sort[i].trim());
			dict.setPtypeCode(sysDict.getPtypeCode());
			dict.setPtypeName(sysDict.getPtypeName());
			toInsertList.add(dict);
		}
		saveBatch(toInsertList, toInsertList.size());
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editSave(SysDict sysDict) throws CommonException {
//		Map<String, Object> map = new HashMap<String, Object>();
		String[] ids = new String[1];
		ids[0] = sysDict.getPtypeCode();
//		map.put("ids", ids);
//		dictManageMapper.deleteDictManageList(map);
		
		String[] code = sysDict.getDictCode().split("\\,");
		String[] name = sysDict.getDictName().split("\\,");
		String[] status = sysDict.getStatus().split("\\,");
		String[] sort = sysDict.getSort().split("\\,");
		List<SysDict> toUpdateList = new ArrayList<>();
		for(int i = 0; i < code.length; i++){
			SysDict data = new SysDict();
			data.setDictCode(code[i].trim());
			data.setDictName(name[i].trim());
			data.setStatus(status[i].trim());
			data.setSort(sort[i].trim());
			data.setPtypeCode(sysDict.getPtypeCode());
			data.setPtypeName(sysDict.getPtypeName());
			toUpdateList.add(data);
		}
		saveOrUpdateBatch(toUpdateList, toUpdateList.size());
	}

	@Override
	public void deleteDictManageList(Map<String, Object> paramMap) throws CommonException {
		dictManageMapper.delete(new LambdaQueryWrapper<SysDict>().eq(SysDict::getDictId, paramMap.get("dictId")).or().eq(SysDict::getPtypeCode, paramMap.get("pcode")));
	}

}
